<!DOCTYPE html>

<html lang="zh-Hans">
  <head>
    <meta charset="utf-8" />
    <meta name="author" content="rogepi" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="/style.css" />
    <script src="/vue.global.js"></script>
    <title>4-provide-inject</title>
  </head>

  <body>
    <div id="provide-inject"></div>
    <script>
      const app = Vue.createApp({});

      app.component('todo-list', {
        data() {
          return {
            todos: ['Feed a cat', 'Buy tickets'],
          };
        },
        provide() {
          return {
            // 处理响应性
            todoLength: Vue.computed(() => this.todos.length),
          };
        },
        template: `
        <div>
          {{ todos.length }}
          <!-- 模板的其余部分 -->
        </div>
      `,
      });

      app.component('todo-list-statistics', {
        inject: ['todoLength'],
        created() {
          console.log(`Injected property: ${this.todoLength.value}`); // > Injected property: 5
        },
      });

      app.mount('#provide-inject');
    </script>
  </body>
</html>
